import {
    CHANGE_SALES_LIST,
    CHANGE_SALES_STATUS,
    CHANGE_DATE,
    CHANGE_STARTDATE,
    CHANGE_ENGDATE,
    CHANGE_SORT_VALUE,
    CHANGE_CUSTOM_CHECKED,
    CHANGE_CUSTOM_SEARCH_VAL,
    CHANGE_SALE_SEARCH_VAL,
    CHANGE_AR_AP_DETAIL_LIST,
    CHANGE_CUSTOM_LIST,
    CHANGE_DETAIL_TOTAL,
    CHANGE_CUSTOM_DETAIL_LIST,
    CHANGE_SALE_SELECTED,
    CHANGE_CUSTOM_SELECTED,
	CHANGE_PROJECT_SEARCH_VAL,
	CHANGE_PROJECT_SELECTED,
	CHANGE_PROJECT_CHECKED,
	CHANGE_PROJECT_LIST,
	CHANGE_MARKETING_SEARCH_VAL,
	CHANGE_MARKETING_SELECTED,
	CHANGE_MARKETING_CHECKED,
	CHANGE_MARKETING_LIST,
	CHANGE_WAREHOUSE_SEARCH_VAL,
	CHANGE_WAREHOUSE_SELECTED,
	CHANGE_WAREHOUSE_CHECKED,
	CHANGE_WAREHOUSE_LIST,    
    CHANGE_MATERIAL_SEARCH_VAL,
    CHANGE_MATERIAL_SELECTED,
    CHANGE_MATERIAL_CHECKED,
    CHANGE_MATERIAL_LIST,    
    CHANGE_DEPT_SEARCH_VAL,
    CHANGE_DEPT_SELECTED,
    CHANGE_DEPT_CHECKED,
    CHANGE_DEPT_LIST,
	CHANGE_ZERO_SELECTED
} from "../mutation-type.js"
import utils from "@/utils/utils.js"
import { getSalesmanList, getCustomList, getArApDetailList,GetProject,GetOrganizations,GetStock, GetMaterial, GetGood,GetDept} from "@/service/api/erp.js"

const report = {
    namespaced: true,
    state: () => ({
        salesmanList: [], // 业务员
        customList: [], // 客户
        projectList: [], // 项目
        marketingList: [], // 销售组织
		warehouseList: [], //仓库列表
        materialList: [], //物料列表
        deptList: [], //部门列表
        startDate: utils.today().startDate, // 报表开始时间
        endDate: utils.today().endDate, // 报表结束时间
        detailList: [], // 往来明细列表
        detailTotal: 0, // 往来明细总数
        customDetailList: [], // 客户详情列表
        selectedSortValue: 0,
        saleSearchVal: "", // 搜索业务员
        customSearchVal: "", // 搜索客户
		projectSearchVal: "", // 搜索项目
		marketingSearchVal: "", // 搜索销售组织
		warehouseSearchVal: "", // 搜索仓库
        materialSearchVal: "", //搜索物料
        deptSearchVal: "", //搜索部门
        selectedSalesman: null, // 选中的业务员
        dataZero: 0, // 是否显示为0的数据
        selectedCustoms: [], // 选中的客户
        selectedProject: [], // 选中的项目
        selectedMarketing: [], // 选中的销售组织
        selectedWarehouse: [], // 选中的仓库
        selectedMaterial: [], //选中的物料
        selectedDept: [], //选中的部门
    }),
    mutations: {
        [CHANGE_SALES_LIST](state, list) {
            if (list.length == 0) {
                state.salesmanList = []
            } else {
                state.salesmanList.push(...list)
            }
        },
        [CHANGE_AR_AP_DETAIL_LIST](state, list) {
            if (list.length == 0) {
                state.detailList = []
            } else {
                state.detailList.push(...list)
            }
        },
        [CHANGE_CUSTOM_LIST](state, list) {
            if (list.length == 0) {
                state.customList = []
            } else {
                state.customList.push(...list)
            }
        },
		[CHANGE_PROJECT_LIST](state, list) {
		    if (list.length == 0) {
		        state.projectList = []
		    } else {
		        state.projectList.push(...list)
		    }
		},
		[CHANGE_MARKETING_LIST](state, list) {
		    if (list.length == 0) {
		        state.marketingList = []
		    } else {
		        state.marketingList.push(...list)
		    }
		},
		[CHANGE_WAREHOUSE_LIST](state, list) {
		    if (list.length == 0) {
		        state.warehouseList = []
		    } else {
		        state.warehouseList.push(...list)
		    }
		},
		[CHANGE_MATERIAL_LIST](state, list) {
		    if (list.length == 0) {
		        state.materialList = []
		    } else {
		        state.materialList.push(...list)
		    }
		},
        [CHANGE_DEPT_LIST](state, list) {
            if (list.length == 0) {
                state.deptList = []
            } else {
                state.deptList.push(...list)
            }
        },
        [CHANGE_DETAIL_TOTAL](state, num) {
            state.detailTotal = num
        },
        [CHANGE_CUSTOM_DETAIL_LIST](state, list) {
            if (list.length == 0) {
                state.customDetailList = []
            } else {
                state.customDetailList.push(...list)
            }
        },
        [CHANGE_SALES_STATUS](state, payload) {
            state.salesmanList.forEach((item) => {
                if (payload.EId == item.EId) {
                    payload.isSelected = !payload.isSelected
                } else {
                    item.isSelected = false
                }
            })
        },
        [CHANGE_SALE_SELECTED](state, payload) {
            if (payload && payload.isSelected) {
                state.selectedSalesman = payload
            } else {
                state.selectedSalesman = null
            }
        },
		[CHANGE_ZERO_SELECTED](state, payload) {
		    state.dataZero = payload==0?1:0
		},
        [CHANGE_CUSTOM_SELECTED](state, payload) {
            state.selectedCustoms = payload.filter((item) => item.isSelected)
        },
		[CHANGE_PROJECT_SELECTED](state, payload) {
		    state.selectedProject = payload.filter((item) => item.isSelected)
		},
		[CHANGE_MARKETING_SELECTED](state, payload) {
		    state.selectedMarketing = payload.filter((item) => item.isSelected)
		},
		[CHANGE_WAREHOUSE_SELECTED](state, payload) {
		    state.selectedWarehouse = payload.filter((item) => item.isSelected)
		},
        [CHANGE_MATERIAL_SELECTED](state, payload) {
            state.selectedMaterial = payload.filter((item) => item.isSelected)
        },
        [CHANGE_DEPT_SELECTED](state, payload) {
            state.selectedDept = payload.filter((item) => item.isSelected)
        },
        [CHANGE_DATE](state, payload) {
            state.startDate = payload.startDate
            state.endDate = payload.endDate
        },
		
		[CHANGE_STARTDATE](state, payload) {
			state.startDate = payload
		},
		[CHANGE_ENGDATE](state, payload) {
			state.endDate = payload
		},
        [CHANGE_SORT_VALUE](state, sortValue) {
            state.selectedSortValue = sortValue
        },
        [CHANGE_CUSTOM_CHECKED](state, payload) {
            state.customList.forEach((item) => {
                if (payload.includes(String(item.UserCode))) {
                    item.isSelected = true
                } else {
                    item.isSelected = false
                }

                state.selectedCustoms.forEach((v) => {
                    if (v.UserCode == item.UserCode) {
                        v.isSelected = item.isSelected
                    }
                })
            })
        },
		[CHANGE_PROJECT_CHECKED](state, payload) {
			state.projectList.forEach((item) => {
				if (payload.includes(String(item.ProjectNo))) {
					item.isSelected = true
				} else {
					item.isSelected = false
				}

				state.selectedProject.forEach((v) => {
					if (v.ProjectNo == item.ProjectNo) {
						v.isSelected = item.isSelected
					}
				})
				
			})
            console.log(state, payload,"项目")
		},
		[CHANGE_MARKETING_CHECKED](state, payload) {
			state.marketingList.forEach((item) => {
				if (payload.includes(String(item.OrganizationsNo))) {
					item.isSelected = true
				} else {
					item.isSelected = false
				}
		
				state.selectedMarketing.forEach((v) => {
					if (v.OrganizationsNo == item.OrganizationsNo) {
						v.isSelected = item.isSelected
					}
				})
				
			})
		},
		[CHANGE_WAREHOUSE_CHECKED](state, payload) {
			state.warehouseList.forEach((item) => {
				if (payload.includes(String(item.StockInfoNo))) {
					item.isSelected = true
				} else {
					item.isSelected = false
				}
		
				state.selectedWarehouse.forEach((v) => {
					if (v.StockInfoNo == item.StockInfoNo) {
						v.isSelected = item.isSelected
					}
				})
				
			})
		},
        [CHANGE_MATERIAL_CHECKED](state, payload) {
        	state.materialList.forEach((item) => {
        		if (payload.includes(String(item.FID))) {
        			item.isSelected = true
        		} else {
        			item.isSelected = false
        		}
        
        		state.selectedMaterial.forEach((v) => {
        			if (v.FID == item.FID) {
        				v.isSelected = item.isSelected
        			}
        		})
        		
        	})
        },
        [CHANGE_DEPT_CHECKED](state, payload) {
        	state.deptList.forEach((item) => {
        		if (payload.includes(String(item.DeptInfoNo))) {
        			item.isSelected = true
        		} else {
        			item.isSelected = false
        		}
        
        		state.selectedDept.forEach((v) => {
        			if (v.DeptInfoNo == item.DeptInfoNo) {
        				v.isSelected = item.isSelected
        			}
        		})
        		
        	})
            console.log(state,payload,"部门")
        },
		
        [CHANGE_CUSTOM_SEARCH_VAL](state, searchValue) {
            state.customSearchVal = searchValue
        },
		[CHANGE_PROJECT_SEARCH_VAL](state, searchValue) {
		    state.projectSearchVal = searchValue
		},
		[CHANGE_MARKETING_SEARCH_VAL](state, searchValue) {
		    state.marketingSearchVal = searchValue
		},
		[CHANGE_WAREHOUSE_SEARCH_VAL](state, searchValue) {
		    state.warehouseSearchVal = searchValue
		},
        
        [CHANGE_SALE_SEARCH_VAL](state, searchValue) {
            state.saleSearchVal = searchValue
        },
        [CHANGE_MATERIAL_SEARCH_VAL](state, searchValue) {
            state.materialSearchVal = searchValue
        },
        [CHANGE_DEPT_SEARCH_VAL](state, searchValue) {
            state.deptSearchVal = searchValue
        },
    },
    actions: {
        async fetchSalesmanList({ commit, state }, payload) {
            return new Promise(async (resolve, reject) => {
                try {
                    const res = await getSalesmanList(payload)
                    const newData = res.Data.map((item) => {
                        let newItem = JSON.parse(JSON.stringify(item))
                        if (state.selectedSalesman && newItem.EId == state.selectedSalesman.EId) {
                            newItem.isSelected = true
                        } else {
                            newItem.isSelected = false
                        }
                        return newItem
                    })
                    // 如果是第一页，需要手动清空列表
                    if (payload.pageNumber == 1) {
                        commit(CHANGE_SALES_LIST, [])
                    }
                    if (res.State == 1) {
                        // 追加新数据
                        commit(CHANGE_SALES_LIST, newData)
                    }
                    resolve(res)
                } catch (error) {
                    reject(error)
                }
            })
        },
        async fetchCustomList({ commit, state }, payload) {
            return new Promise(async (resolve, reject) => {
                try {
                    const res = await getCustomList(payload)
                    const newData = res.Data.map((item) => {
                        let newItem = JSON.parse(JSON.stringify(item))
                        if (state.selectedCustoms.length > 0) {
                            state.selectedCustoms.forEach((custom) => {
                                if (newItem.UserCode == custom.UserCode) {
                                    newItem.isSelected = true
                                }
                            })
                        } else {
                            newItem.isSelected = false
                        }
                        return newItem
                    })
                    // 如果是第一页，需要手动清空列表
                    if (payload.pageNumber == 1) {
                        commit(CHANGE_CUSTOM_LIST, [])
                    }
                    if (res.State == 1) {
                        // 追加新数据
                        commit(CHANGE_CUSTOM_LIST, newData)
                    }
                    resolve(res)
                } catch (error) {
                    reject(error)
                }
            })
        },
		async fetchProjectList({ commit, state }, payload) {
		    return new Promise(async (resolve, reject) => {
		        try {
		            const res = await GetProject(payload)
		            const newData = res.Data.map((item) => {
		                let newItem = JSON.parse(JSON.stringify(item))
		                if (state.selectedProject.length > 0) {
		                    state.selectedProject.forEach((custom) => {
		                        if (newItem.ProjectNo == custom.ProjectNo) {
		                            newItem.isSelected = true
		                        }
		                    })
		                } else {
		                    newItem.isSelected = false
		                }
		                return newItem
		            })
		            // 如果是第一页，需要手动清空列表
		            if (payload.pageNumber == 1) {
		                commit(CHANGE_PROJECT_LIST, [])
		            }
		            if (res.State == 1) {
		                // 追加新数据
		                commit(CHANGE_PROJECT_LIST, newData)
		            }
		            resolve(res)
		        } catch (error) {
		            reject(error)
		        }
		    })
		},
		async fetchMarketingList({ commit, state }, payload) {
		    return new Promise(async (resolve, reject) => {
		        try {
		            const res = await GetOrganizations(payload)
		            const newData = res.Data.map((item) => {
		                let newItem = JSON.parse(JSON.stringify(item))
		                if (state.selectedMarketing.length > 0) {
		                    state.selectedMarketing.forEach((custom) => {
		                        if (newItem.OrganizationsNo == custom.OrganizationsNo) {
		                            newItem.isSelected = true
		                        }
		                    })
		                } else {
		                    newItem.isSelected = false
		                }
		                return newItem
		            })
		            // 如果是第一页，需要手动清空列表
		            if (payload.pageNumber == 1) {
		                commit(CHANGE_MARKETING_LIST, [])
		            }
		            if (res.State == 1) {
		                // 追加新数据
		                commit(CHANGE_MARKETING_LIST, newData)
		            }
		            resolve(res)
		        } catch (error) {
		            reject(error)
		        }
		    })
		},
		async fetchWarehouseList({ commit, state }, payload) {
			return new Promise(async (resolve, reject) => {
				try {
					const res = await GetStock(payload)
					const newData = res.Data.map((item) => {
						let newItem = JSON.parse(JSON.stringify(item))
						if (state.selectedWarehouse.length > 0) {
							state.selectedWarehouse.forEach((custom) => {
								if (newItem.StockInfoNo == custom.StockInfoNo) {
									newItem.isSelected = true
								}
							})
						} else {
							newItem.isSelected = false
						}
						return newItem
					})
					// 如果是第一页，需要手动清空列表
					if (payload.pageNumber == 1) {
						commit(CHANGE_WAREHOUSE_LIST, [])
					}
					if (res.State == 1) {
						// 追加新数据
						commit(CHANGE_WAREHOUSE_LIST, newData)
					}
					resolve(res)
				} catch (error) {
					reject(error)
				}
			})
		},
        async fetchMaterialList({ commit, state }, payload) {
        	return new Promise(async (resolve, reject) => {
        		try {
        			const res = await GetGood(payload)
        			const newData = res.Data.map((item) => {
        				let newItem = JSON.parse(JSON.stringify(item))
        				if (state.selectedMaterial.length > 0) {
        					state.selectedMaterial.forEach((custom) => {
        						if (newItem.FID == custom.FID) {
        							newItem.isSelected = true
        						}
        					})
        				} else {
        					newItem.isSelected = false
        				}
        				return newItem
        			})
        			// 如果是第一页，需要手动清空列表
        			if (payload.pageNumber == 1) {
        				commit(CHANGE_MATERIAL_LIST, [])
        			}
        			if (res.State == 1) {
        				// 追加新数据
        				commit(CHANGE_MATERIAL_LIST, newData)
        			}
        			resolve(res)
        		} catch (error) {
        			reject(error)
        		}
        	})
        },
        async fetchDeptList({ commit, state }, payload) {
        	return new Promise(async (resolve, reject) => {
        		try {
        			const res = await GetDept(payload)
        			const newData = res.Data.map((item) => {
        				let newItem = JSON.parse(JSON.stringify(item))
        				if (state.selectedDept.length > 0) {
        					state.selectedDept.forEach((custom) => {
        						if (newItem.DeptInfoNo == custom.DeptInfoNo) {
        							newItem.isSelected = true
        						}
        					})
        				} else {
        					newItem.isSelected = false
        				}
        				return newItem
        			})
        			// 如果是第一页，需要手动清空列表
        			if (payload.pageNumber == 1) {
        				commit(CHANGE_DEPT_LIST, [])
        			}
        			if (res.State == 1) {
        				// 追加新数据
        				commit(CHANGE_DEPT_LIST, newData)
        			}
        			resolve(res)
                    console.log(res,newData,"7777")
        		} catch (error) {
        			reject(error)
        		}
        	})
        },
        async fetchArApDetailList({ commit }, payload) {
            return new Promise(async (resolve, reject) => {
                try {
                    const res = await getArApDetailList(payload)
                    // 如果是第一页，需要手动清空列表
                    if (payload.pageNumber == 1) {
                        commit(CHANGE_AR_AP_DETAIL_LIST, [])
                    }
                    if (res.State == 1) {
                        // 追加新数据
                        commit(CHANGE_AR_AP_DETAIL_LIST, res.Data)
                        commit(CHANGE_DETAIL_TOTAL, res.RecordAmout)
                    }
                    resolve(res)
                } catch (error) {
                    reject(error)
                }
            })
        },
        async fetchCustomDetailList({ commit }, payload) {
            return new Promise(async (resolve, reject) => {
                try {
                    const res = await getArApDetailList(payload)
                    // 如果是第一页，需要手动清空列表
                    if (payload.pageNumber == 1) {
                        commit(CHANGE_CUSTOM_DETAIL_LIST, [])
                    }
                    if (res.State == 1) {
                        // 追加新数据
                        commit(CHANGE_CUSTOM_DETAIL_LIST, res.Data)
                    }
                    resolve(res)
                } catch (error) {
                    reject(error)
                }
            })
        },
    },
}

export default report
